package com.farm.device.service.impl;

import com.farm.device.dao.DeviceMapper;
import com.farm.device.pojo.DeviceInfo;
import com.farm.device.service.DeviceService;
import com.farm.device.pojo.Device;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.persistence.Id;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public List<Device> findAll() {
        return deviceMapper.selectAll();
    }

    @Override
    public Device findById(Integer id) {
        return deviceMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Device> findByName(Device device) {
        Example example = new Example(Device.class);

        Example.Criteria criteria = example.createCriteria();

        if (device.getName()!=null){
            criteria.andEqualTo("name",device.getName());
        }
        List <Device> deviceList = deviceMapper.selectByExample(example);
        return deviceList;
    }

    @Override
    public void delete(Integer id) {
        int i = deviceMapper.deleteByPrimaryKey(id);
        if (i==0){
            throw new RuntimeException("删除失败");
        }

    }

    @Override
    public void add(Device device) {
        //设备编码不可重复
        Device device2 = new Device();
        device2.setCode(device.getCode());
        Device device1 = deviceMapper.selectOne(device2);
        if (device1==null) {
            deviceMapper.insert(device);
        }else {
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public void update(Device device) {
        int i = deviceMapper.updateByPrimaryKeySelective(device);
        if (i==0){
            throw new RuntimeException("修改失败");
        }
    }

    @Override
    public Page<Device> findPage(Map<String, Object> map, Integer page, Integer size) {

        PageHelper.startPage(page,size);

        Example example=new Example(Device.class);
        Example.Criteria criteria = example.createCriteria();

        if (map!=null){
//            设备名称模糊查询
            if (map.get("name")!=null||"".equals(map.get("name"))){

                criteria.andLike("name","%"+map.get("name")+"%");
            }

//            设备ID查询
            if (map.get("code")!=null||"".equals(map.get("code"))){
                criteria.andEqualTo("code", map.get("code"));
            }
//            设备类型
            if (map.get("type")!=null||"".equals(map.get("type"))){
                criteria.andEqualTo("type",map.get("type"));
            }


//            根据价格区间查询

            if (map.get("min")!=null||"".equals(map.get("min"))){


                 criteria.andGreaterThanOrEqualTo("price",map.get("min"));

            }

            if (map.get("max")!=null||"".equals(map.get("max"))){

                criteria.andGreaterThanOrEqualTo("price",map.get("max"));
            }
//          按照品牌查询
            if (map.get("brand")!=null||"".equals(map.get("brand"))){
                criteria.andLike("brand","%"+map.get("brand")+"%");
            }
//          按照型号查询
            if (map.get("model")!=null||"".equals(map.get("model"))){
                criteria.andEqualTo("model",map.get("model"));
            }

//          按照应用场景查询
            if (map.get("scenarios")!=null||"".equals(map.get("scenarios"))){
                criteria.andEqualTo("scenarios",map.get("scenarios"));
            }
        }


         Page<Device> pageInfo= (Page <Device>) deviceMapper.selectByExample(example);
        return pageInfo;
    }


}
